# 执行pip install matplotlib
# 依赖使用示例2中定义的类
# 本案例验安装matplotlib不成功，不能正常运行
import matplotlib.pyplot as plt
from 使用示例2 import DiscreteMath
dm = DiscreteMath()

# 获取某个文件某个学科段的成绩
def get_scores(filename,col_name):
    dm.set_path(filename)
    scores = dm.get_col_of_data(col_name)
    return dm.histogram_of_data(scores)
# 获取某个元素在数组中的坐标
def find_index(arr,elm):
    i=0
    for item in arr:
        if item == elm:
            return i
        i +=1
    return -1
 # 文件名
file_names=["d:/项目/python/爬虫/表格/信息技术成绩.xlsx","d:/项目/python/爬虫/表格/数据技术成绩.xlsx"]
classes=["15A","15B"]
colors=["b","r"]
term_name="数学"
# 获取成绩并绘制
index = range(101)
width = 0.25
fig,ax=plt.subplots()
opacity = 0.9
for fn in file_names:
    term_scores = get_scores(fn,term_name)
    idx = find_index(file_names,fn)
    if idx == -1:
        continue
    # 绘制直方图
    ax.bar(index+idx*width,term_scores,width=width,alpha=opacity,label=classes[idx],color=colors[idx])
ax.set_xlabel("Score")
ax.set_ylabel("Persons")
ax.set_title("The score compared between two classes")
ax.legend(loc="upper left")
fig.tight_layout()
plt.show()